//
//  MyPostViewController.m
//  唧歪
//
//  Created by ZC on 15/7/8.
//  Copyright (c) 2015年 MWJ. All rights reserved.
//

#import "MyPostViewController.h"
#import "myPostCell.h"
#import "PostDetailsViewController.h"  //帖子详情界面
#import "AppDelegate.h"
#import "myPost.h"
#define myPostURL @"https://app.jiwaipro.com:8888/index.php/JiWai/seek_up"  //我的帖子
#define myPost_DXURL @"https://app.jiwaipro.com:8888/index.php/JiWai/mypost_dx"  //“我的帖子”倒叙排列

@interface MyPostViewController (){
    NSMutableArray *post_DXArray;
    NSMutableArray *myPostArray;
    NSMutableArray *Array;
    NSMutableArray *DX_Array;
    AppDelegate *app;
    NSMutableArray *tagArray;
    int pageNumber;
    int pageNumber1;
    int DX_int;
}

@end

@implementation MyPostViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"我的帖子";
    
    //创建右边菜单按钮
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"正倒叙.png"] style:UIBarButtonItemStylePlain target:self action:@selector(rightAction)];
    self.navigationItem.rightBarButtonItem = item;
    
    myPostArray=[NSMutableArray array];
    post_DXArray = [NSMutableArray array];
    Array=[NSMutableArray array];
    tagArray=[NSMutableArray array];
    DX_Array = [NSMutableArray array];
    app=(AppDelegate *)[[UIApplication sharedApplication]delegate];
    app.quickOrNot =@"0";
    pageNumber = 1;
    pageNumber1 = 1;
    DX_int = 0;
    
    self.tableView.footer = [[KSDefaultFootRefreshView alloc] initWithDelegate:self]; //上拉刷新
    self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    
    [app isConnectionAvailable];
    if ([app.Existence isEqualToString:@"1"]) {
    }else{
    [self addDXData];
        app.Existence = @"0";
    }
}

- (void)refreshViewDidLoading:(id)view
{
    if ([view isEqual:self.tableView.footer]) {
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            if (Array.count >= [myPostArray[myPostArray.count-1] intValue]) {
                
                [self.tableView.footer setIsLastPage:YES];
                [self.tableView reloadData];
            } else {
                if (DX_int == 1){
                ++pageNumber1;
                }else{
                ++pageNumber;
                }
                [self addDXData];
                [self.tableView.footer setState:RefreshViewStateDefault];
                [self.tableView reloadData];
            }
        });
    }
}
-(void)rightAction{
    myPostArray=[NSMutableArray array];
    Array = [NSMutableArray array];
    if (DX_int == 0) {
        DX_int = 1;
        pageNumber1 = 1;
        [self addDXData];
    }else{
        DX_int = 0;
        pageNumber = 1;
        [self addDXData];
    }
}
-(void)addDXData{
    if (DX_int == 1) {
        NSLog(@"%d",pageNumber1);
        NSString *poststr =[NSString stringWithFormat:@"user_id=%@&pg=%d",app.user_id,pageNumber1];
        [self londingPath1:myPost_DXURL Str1:poststr];
    }else{
        NSLog(@"%d",pageNumber);
        NSString *poststr =[NSString stringWithFormat:@"user_id=%@&pg=%d",app.user_id,pageNumber];
        [self londingPath1:myPostURL Str1:poststr];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

#pragma mark - 请求帖子列表数据
-(void)londingPath1:(NSString *)path Str1:(NSString *)string{
    NSURL *url = [NSURL URLWithString:path];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPMethod:@"POST"];
    NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
    [request setHTTPBody:data];
    [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        myPostArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        [self performSelectorOnMainThread:@selector(refreshData1:) withObject:myPostArray waitUntilDone:YES];
    }];
}

-(void)refreshData1:(NSMutableArray *)sender1{
    for (int i=0; i<(sender1.count-1); i++) {
        myPost *p = [[myPost alloc]init];
        p.post_content = sender1[i][@"message_content"];
        p.post_time = sender1[i][@"message_time"];
        p.post_showTime = sender1[i][@"message_showtime"];
        p.post_id=sender1[i][@"message_id"];
        p.post_tag=sender1[i][@"message_tag"];
        p.post_commentNumber=sender1[i][@"message_comment"];
        
        [Array addObject:p];
    }
    [self.tableView reloadData];
}

#pragma mark - 返回tableview的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return Array.count;
}

#pragma mark - 返回tableview的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 90;
}
#pragma mark - cell的内容加载
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UINib *nib=[UINib nibWithNibName:@"myPostCell" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:@"myPostCell"];
    
    static NSString *cellIdentifier=@"myPostCell";
    myPostCell *cell=[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    myPost *myPost=Array[indexPath.row];
    
    cell.time.text=[NSString stringWithFormat:@"%@",myPost.post_time];  //发布时间
    cell.number.text=[NSString stringWithFormat:@"%@",myPost.post_commentNumber];  //回帖数
    
    if ([myPost.post_tag isEqualToString:@"0"]) {
        cell.text.hidden = NO;
        cell.text.text=[NSString stringWithFormat:@"%@",myPost.post_content];
        cell.text.textColor = [UIColor blackColor];
        cell.audioView.hidden = YES;
        
    }else if ([myPost.post_tag  isEqualToString:@"1"]){
        cell.text.hidden = NO;
        cell.text.text = @"图片帖子";
        cell.text.textColor = [UIColor brownColor];
        cell.audioView.hidden = YES;
        
    }else{
        cell.text.hidden = YES;
        cell.audioView.hidden = NO;
        
    }

    cell.messageID = [NSString stringWithFormat:@"%@",myPost.post_id];
    cell.messageTag = [NSString stringWithFormat:@"%@",myPost.post_tag];
    
    
    //设置cell的选中时候的颜色
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    
    return cell;
}

#pragma mark - cell的点击事件
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    myPost *myPost=Array[indexPath.row];
    self.messageID = [NSString stringWithFormat:@"%@",myPost.post_id];
    app.appmessageid = self.messageID;
    
    PostDetailsViewController *postDetail=[self.storyboard instantiateViewControllerWithIdentifier:@"detailVC"];
    [self.navigationController pushViewController:postDetail animated:YES];

}

@end
